import {
  getFoodDetail,
  permissionToCanteenName,
  permissionToCanteenId,
  getFoodClass,
  findCanteenNameById
} from "../../apis/apis"
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    canteenId:0,
    foodId:0,
    foodClassId:0,
    imageUrl: "",
    title: "",
    foodClassName: "",
    comment: 0,
    price: 0,
    description: "",
    tags:"",
    canteenName: "第N饭堂",
    pickerIndex: 0,
    array: [],
    foodClassArr:[],
    time:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    wx.setNavigationBarTitle({
      title: '悦食生活-菜品详情修改',
    })
    await getFoodDetail(options.id).then(res => {
      console.log(res);
      this.setData({
        canteenId:res.canteenId,
        comment:res.comment,
        description:res.description,
        foodClassId:res.foodClassId,
        foodId:res.id,
        imageUrl:res.imageUrl,
        price:res.price,
        tags:res.tags,
        time:res.time,
        title:res.title,
        timePicker:parseInt(res.time - 1),
        canteenName:findCanteenNameById(res.canteenId,app.globalData.canteenList)
      })

    })
    await getFoodClass(this.data.canteenId).then(res=>{
      console.log(res);
      let arrTemp=[""]
      for (let index = 0; index < res.length; index++) {
        arrTemp[index]=res[index].foodClassName
      }
      this.setData({
        array:arrTemp,
        foodClassArr:res
      })
      for (let index = 0; index < arrTemp.length; index++) {
        if(res[index].id==this.data.foodClassId){
          this.setData({
            foodClassName:res[index].foodClassName,
            pickerIndex:index
          })
          console.log(this.data.pickerIndex);
        }
        
      }
    })

  },

  pickerChange: function (e) {
    this.setData({
      pickerIndex: e.detail.value
    })
  }, 
  timePickerChange(e){
    this.setData({
      timePicker: e.detail.value
    })
  },
  titleChange:function(e){
    this.setData({
      title:e.detail.value
    })
  },
  urlChange:function(e){
    this.setData({
      imageUrl:e.detail.value
    })
  },
  priceChange:function(e){
    this.setData({
      price:e.detail.value
    })
  },
  timeChange:function(e){
    this.setData({
      time:e.detail.value
    })
  },
  tagsChange:function(e){
    this.setData({
      tags:e.detail.value
    })
  },
  saveChange(){
    wx.request({
      url: app.globalData.apiUrl+'/food/modifyFood',
      method:"POST",
      data:{
        "token":wx.getStorageSync('token'),
        "id":this.data.foodId,
        "title":this.data.title,
        "imageUrl": this.data.imageUrl,
        "price":this.data.price,
        "food_class_id":this.data.foodClassArr[this.data.pickerIndex].id,
        "canteen_id":this.data.canteenId,
        "time": this.data.time,
        "tags":this.data.tags
      },
      success:(res)=>{
        console.log(res);
        if(res.data.code==20000){
          wx.showToast({
            title: '修改成功',
          })
        }

      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})